import menu from '../aside/Menu.js';
import { mapGetters } from "vuex";

export default {
    name: 'Tag',
    data() {
        return {


        }
    },
    computed: {
        ...mapGetters(["getTags","getChecked"])
    },
    watch: {
        $route () {
            // todo 判断是否要缓存
            // 添加tag 或者 切换选中菜单
            this.$store.commit('setTags',this.$route);
            this.$store.commit('setCrumb',this.breadcrumb(menu, this.$route.fullPath));
        }
    },
    methods: {
        close(tag){
            this.$store.commit('removeTags',tag);
        },
        checked(tag){
            // 同样的地址，不能再次请求
            if (tag.path !== this.$route.fullPath){
                this.$router.push({path: tag.fullPath});
            }
        },
        //  获取面包屑的函数
        breadcrumb(data, path){
            let crumb = [];
            for (const menu of data) {
                if (menu.children.length > 0){
                    crumb = crumb.concat(this.breadcrumb(menu.children, path));
                }else {
                    if (menu.path === path){
                        return [menu.title];
                    }
                }
                if (crumb.length > 0){
                    return [menu.title].concat(crumb);
                }
            }
            return crumb;
        }
    },
    mounted() {
        // 刷新页面重新加载计算 面包屑
        this.$store.commit('setCrumb',this.breadcrumb(menu, this.$route.path));
    }
}
